<?php
  
  class filemanager{
    
    static function treefilelist($dir) {
      $files = self::tree2files(self::tree($dir));
      return $files;
    }
    
    static function tree2files($tree, $dir = ''){
      $files = array();
      foreach ($tree as $key => $file) {
        if (is_array($file)) {
          $files = array_merge($files, self::tree2files($file, $dir !== '' ? ($dir.'/'.$key) : $key));
        } else {
          $files[] = $dir . '/' . $file;
        }
      }
      return $files;
    }
    
    static function tree($dir) {
      $files = array();
      $scan_result = scandir($dir);
      foreach ($scan_result as $key => $filepath) {
        if ($filepath === '..' || $filepath === '.') {
        } else if (is_dir($dir . '/' . $filepath)){
          $files[$filepath] = self::tree($dir . '/' . $filepath);
        } else {
          $files[] = $filepath;
        }
      }
      return $files;
    }
    /*
    static function filelist($dir) {
      $files = array();
      $scan_result = scandir($dir);
      foreach ($scan_result as $key => $filepath) {
        if (is_file($dir . '/' . $filepath)) {
          $files[] = $filepath;
        }
      }
      return $files;
    }
    
    static function dirlist($dir) {
      $files = array();
      $scan_result = scandir($dir);
      foreach ($scan_result as $key => $filepath) {
        if (is_dir($dir . '/' . $filepath) && $filepath !== '.' && $filepath !== '..') {
          $files[] = $filepath;
        }
      }
      return $files;
    }
    */
    static function ext_replace($e1, $e2, $path) {
      return str_replace('.'.$e1, '.'.$e2, $path);
    }
  }